package com.ruoyi.help.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class EnterpriseWeChatRobot {

    private static String WEBHOOK_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fece9607-3f00-4553-9407-c84f3490661c";

    public static void sendMan(String name) {
        String text = "@" + name + "\n  请帮忙处理帮扶任务!\n    点击连接前往帮扶：http://localhost/helpJob/helpTransfer";//发送的文本消息
        String mentionedList = "@lejav";//需要@的成员列表，多个成员用“|”分隔
        String messageURL = "";
        String picURL = "";
        String postData = "{\"msgtype\": \"text\","
                + "\"text\": {\"content\": \"" + text + "\","
                + "\"mentioned_list\":\"" + mentionedList + "\"},"
                // + "\"mentionedList\":\"" + mentionedList + "\","
                + "\"messageURL\":\"" + messageURL + "\","
                + "\"picURL\":\"" + picURL + "\"}";
        HttpURLConnection conn = null;
        try {
            URL url = new URL(WEBHOOK_TOKEN);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setDoOutput(true);
            conn.getOutputStream().write(postData.getBytes("utf-8"));
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            conn.disconnect();
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void sendMan(String name, String title, String u) {
        String text = "@" + name + "\n" + title + "\n" + u;//发送的文本消息
        String mentionedList = "@lejav";//需要@的成员列表，多个成员用“|”分隔
        String messageURL = "";
        String picURL = "";
        String postData = "{\"msgtype\": \"text\","
                + "\"text\": {\"content\": \"" + text + "\","
                + "\"mentioned_list\":\"" + mentionedList + "\"},"
                // + "\"mentionedList\":\"" + mentionedList + "\","
                + "\"messageURL\":\"" + messageURL + "\","
                + "\"picURL\":\"" + picURL + "\"}";
        HttpURLConnection conn = null;
        try {
            URL url = new URL(WEBHOOK_TOKEN);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setDoOutput(true);
            conn.getOutputStream().write(postData.getBytes("utf-8"));
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            conn.disconnect();
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
